/**
 * service_address js file;
 * company:junqitecch
 * author:xuzuiotao
 * 
 */

//test
//
// LOGOUT:32,
//        USER_DETAIL:33,
//        USER_CHANGE_PASSWORD:34,


/**
 * login js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */

var  consts  = require('../consts');
var address= require('../db/address');
var db= require('../db/dbconnection');


var cache=require('../redis/usercache');
var bind= require('../db/bind');
var sms = require('../network/httpsms');
var checkSMS = require('../db/mphonemsg');
var im= require('../network/im');
var qiniu=require('../network/qiniu');
var crypto=require('../utils/crypto');
var oauth2= require('../network/oauth2');
var member= require('../db/member');

async function processAddressEdit(request,response,callback){
	
	var data =request.data;
	var muuid = null;
	var code = null;
	console.log('processAddressEdit::'+ data+':'+request.muuid);
	var isMA = 0;
	var type = null;

	var ID = null;
	var toDelete= 0;
	var detail = null;
	var tele = null;
	var fullName = null;
	var isDefault =0;
	var regionIDs = null;
	var uregionID = 0;

	var region = null;
	
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return;
	}

	var param=null;
	try {
	    param = JSON.parse(data);

	} catch (e) {
	}
	
	muuid = request.muuid;
	
	if(param===null || typeof(param.type)==='undefined' || typeof(param.authCode)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'type and authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.type !==1 && param.type!==2){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':type';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.telephone){
	   tele = param.telephone;
	}
	
	if(param.region){
		region = param.region;
		}
	if(param.detail){
		detail = param.detail;
	}
	
	if(param.toDelete){
		toDelete = param.toDelete;
	}
	
	if(param.fullName){
		fullName = param.fullName;
	}
	
	if(param.ID){
		ID = param.ID;
	}
	
	if(param.regionIDs){
		regionIDs = param.regionIDs;
	}
	if(param.uregionID){
		uregionID = param.uregionID;
	}
	
	if(param.isDefault){
		isDefault = param.isDefault;
	}
	
	
	if(typeof(param.authCode)==='undefined' || param.authCode===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	type = param.type;
	code = param.authCode;

	
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}

	var conn = await db.createDB();
	
	if( !conn){
		response.errorCode=consts.Error.ERROR_BIND_ERROR;
		response.message=consts.Message.ERROR_BIND_ERROR;
		if(callback){
			callback(response);
		}
		return false;
	}
	
	var obj = await member.fetchAppIDandOpenID1(conn,muuid,code,type,null);//, function(obj){

	if(obj===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}

		db.releaseDB(conn);

		return;
	}
		
	console.log('fetchAppIDandOpenID::'+ JSON.stringify(obj));

		
	var entity = null;
	
	if(!obj.openID){
		var err = null;
		db.releaseDB(conn);
		var appid = obj.appid;
		var secret = obj.appsecret;
		if(type===1){
			if(isMA===1){
				appid = obj.g_appid;
				secret = obj.g_appsecret;
			}
			try {
				obj.openID = await oauth2.loginWeixin(appid, secret, code,isMA,null);
			}catch(err0){
				err = err0;
			}	
			
		}else if(type===2){
					
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
			try{
				obj.openID = await oauth2.loginZfb(appid, secret, code,null);
			}catch(err0){
				err = err0;
			}
		}
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		conn = await db.createDB();
		
		if(!conn){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
			return false;
		}
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type, null);
		var mmm = await member.check1(conn,muuid,obj.openID, type,1, null);
		
	}
	var entity = await address.addOrUpdate1(conn,muuid,obj.openID, type,ID,toDelete,regionIDs,uregionID,detail,tele, fullName,isDefault, region, null);//{
		
	db.releaseDB(conn);

	if(entity===null ){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
	}else{
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(entity);

		if(callback){
			callback(response);
		}
	}
			
}

async function processDetail(request,response,callback){

	console.log('processDetail');
	var ID =0;
	var data =request.data;
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		
		return;
	}

	var param=null;
	try {
	    param = JSON.parse(data);

	} catch (e) {
	}
	
	var muuid= request.muuid;
	
	var code = null;
	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'';
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.ID){
		ID= Number(param.ID);
	}

	
	if(ID >0){
	
	}else{
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'ID';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	var conn = await db.createDB();
	if(!conn){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'ID';
		if(callback){
			callback(response);
		}
		return false;
	}
	var obj = await	address.detail1(conn,ID, null);//function(obj){
	db.releaseDB(conn);

	if(obj===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'ID';
		if(callback){
			callback(response);
		}

		return;
	}
	
	response.errorCode = consts.Error.NONE_ERROR;
	response.message= consts.Message.OK_MESSAGE;
	response.data = JSON.stringify(obj);
	if(callback){
		callback(response);
	}

}

 


async function processList(request, response,callback){
	var data =request.data;
	var muuid = null;
	var code = null;
	var isMA = 0;
	var isDefault= 0;
	console.log('processRegister::'+ data);
	
	if(request.muuid===null || typeof(request.muuid)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':muuid';
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param=null;
	try {
	    param = JSON.parse(data);
	
	} catch (e) {
	}
	
	muuid = request.muuid;
	
	if(param===null || typeof(param.type)==='undefined' || typeof(param.authCode)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+'type and authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.type !==1 && param.type!==2){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':type';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	if(typeof(param.authCode)==='undefined' || param.authCode===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':authCode';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	
	if(param.isMA !==null && typeof(param.isMA)!=='undefined' && param.isMA ===1){
		isMA = 1;
	}
	
	if(param.isDefault && param.isDefault===1){
		isDefault= 1;
	}
	
	var type = param.type;
	code = param.authCode;

	var conn = await db.createDB();
	if(!conn ){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		return false;
	}
	var obj = await	member.fetchAppIDandOpenID1(conn,muuid,code,type, null);//function(obj){
	
	if(obj===null){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}

		db.releaseDB(conn);

		return;
	}
	
	console.log('fetchAppIDandOpenID::'+ JSON.stringify(obj));
	
	if(!obj.openID){	
					
		db.releaseDB(conn);
		conn = null;
		var err=null;
		var appid = obj.appid;
		var secret = obj.appsecret;
			
		if(type ===1){
			if(isMA===1){
				appid = obj.g_appid;
				secret = obj.g_appsecret;
			}
			try{
				obj.openID = await oauth2.loginWeixin(appid, secret, code,isMA,null);
			}catch(e){
				err= e;
			}
		}else if(type===2){
						
			appid = obj.alappid;
			secret = null;//obj.g_appsecret;
						
			try{
				obj.openID = await oauth2.loginZfb(appid, secret, code,null);
			}catch(e){
				err= e;
			}
				
		}
		if(err){
			response.errorCode=err.number;
			response.message=err.message;
			if(callback){
				callback(response);
			}
			return;
		}
		
		conn = await db.createDB();
		if(!conn ){
			response.errorCode=consts.Error.ERROR_GENERAL;
			response.message=consts.Message.UNKNOWN;
			if(callback){
				callback(response);
			}
			return false;
		}
							
		var oo = await member.saveCodeTemp1(conn,code,obj.openID,type, null);//function(oo){
									
		var mmm = await member.check1(conn,muuid,obj.openID, type,1, null);//function(mmm){

	}				
				
				// register1(conn,muuid,openid,type,phone,password,name,birth,gender, callback){
	var entity = await address.list1(conn,muuid,obj.openID, type,isDefault,null);
	
	db.releaseDB(conn);

	if(entity===null){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
	}else{
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(entity);

		if(callback){
			callback(response);
		}
	}
	
}



async function processRegionList(request, response,callback){
	var data =request.data;
	
	
	console.log('processRegionList::'+ data);
	
	var param=null;
	try {
	    param = JSON.parse(data);
	
	} catch (e) {
	}
	
	var muuid = request.muuid;
	
	var conn = await db.createDB();
	if(!conn){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
		return false;
		
	}
	
	var wrap = await address.listRegion1(conn,muuid);//,function(wrap){
			
	db.releaseDB(conn);
	if(wrap===null){
		response.errorCode=consts.Error.ERROR_GENERAL;
		response.message=consts.Message.UNKNOWN;
		if(callback){
			callback(response);
		}
	}else{
		response.errorCode = consts.Error.NONE_ERROR;
		response.message= consts.Message.OK_MESSAGE;
		response.data = JSON.stringify(wrap);

		if(callback){
			callback(response);
		}
	}

}

module.exports = {
		processRegionList:processRegionList,
		processAddressEdit:processAddressEdit,
		processDetail:processDetail,
		processList:processList,

	  };
